﻿using System;
using System.Collections;
using java.util;

namespace MLSharp.Weka.Interop
{
	/// <summary>
	/// Provides an adapter that can convert a Java
	/// Enumeration class into something that implements
	/// IEnumerator.  
	/// </summary>
	public class EnumeratorEnumerationAdapter : IEnumerator
	{
		#region Private Fields

		/// <summary>
		/// The class being adapted.
		/// </summary>
		private Enumeration mEnumeration;

		/// <summary>
		/// The current object.
		/// </summary>
		private object mCurrent;

		#endregion

		#region Implementation of IEnumerator

		/// <summary>
		/// Advances the enumerator to the next element of the collection.
		/// </summary>
		/// <returns>
		/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
		/// </returns>
		/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
		public bool MoveNext()
		{
			if (!mEnumeration.hasMoreElements())
			{
				return false;
			}

			mCurrent = mEnumeration.nextElement();
			return true;
		}

		/// <summary>
		/// Sets the enumerator to its initial position, which is before the first element in the collection.
		/// </summary>
		/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
		public void Reset()
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Gets the current element in the collection.
		/// </summary>
		/// <returns>
		/// The current element in the collection.
		/// </returns>
		/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.-or- The collection was modified after the enumerator was created.</exception><filterpriority>2</filterpriority>
		public object Current
		{
			get
			{
				return mCurrent;
			}
		}

		#endregion

		#region Public Constructors

		/// <summary>
		/// Creates an adapter for the specified enumeration.
		/// </summary>
		/// <param name="enumeration"></param>
		public EnumeratorEnumerationAdapter(Enumeration enumeration)
		{
			mEnumeration = enumeration;
			mCurrent = null;
		}

		#endregion
	}
}
